package br.com.klis.batendoumabola.client.widgets;

import br.com.klis.batendoumabola.client.util.DadosDaImagemClientUtil;
import br.com.klis.batendoumabola.shared.DadosDaImagem;
import br.com.klis.batendoumabola.shared.LoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ImagemPanel extends Composite {
	private VerticalPanel uploadPanel = new VerticalPanel();
	private FormPanel form = new FormPanel();
	private Button submit = new Button("Submit");
	private Galeria galeria;
	private FileUpload upload = new FileUpload();

	public ImagemPanel(String entidadeId, LoginInfo loginInfo){
		GWT.log("init ImagemPanel...");
		galeria = new Galeria(loginInfo, new Long(entidadeId));
		
		if (entidadeId != null && entidadeId != ""){
			galeria.loadGaleria();
			
			uploadPanel.add(galeria);
			uploadPanel.add(new HTML("<br/>"));
			
		    uploadPanel.setSpacing(1);
			TextBox entidadeIdTextBox = new TextBox();
			entidadeIdTextBox.setName("entidadeId");
			entidadeIdTextBox.setValue(entidadeId);
			entidadeIdTextBox.setVisible(false);
			uploadPanel.add(entidadeIdTextBox);
		    
		    upload.setName("uploadFormElement");
		    uploadPanel.add(upload);
		    
		    submit.setStyleName("botao");
		    uploadPanel.add(submit);
		    
		    form.setWidget(uploadPanel);
		    
			 // Because we're going to add a FileUpload widget, we'll need to set the
		    // form to use the POST method, and multipart MIME encoding.
		    form.setEncoding(FormPanel.ENCODING_MULTIPART);
		    form.setMethod(FormPanel.METHOD_POST);
		    
		    //form.setAction(GWT.getModuleBaseURL()+"uploadblob");
		    form.setAction(GWT.getHostPageBaseURL()+"uploadblob");
			
		    submit.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					GWT.log("Button submit has been pressed.");
					form.submit();
				}
			});
		    
		    form.addSubmitHandler(new FormPanel.SubmitHandler() {
		      public void onSubmit(SubmitEvent event) {
		    	  GWT.log("The form has been submited: "+form.getAction());
		        // This event is fired just before the form is submitted. We can take
		        // this opportunity to perform validation.
		        /*if (tb.getText().length() == 0) {
		          Window.alert("The text box must not be empty");
		          event.cancel();
		        }*/
		      }
		    });
			
		    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
		      public void onSubmitComplete(SubmitCompleteEvent event) {
		    	  GWT.log("The form submit has been completed.");
		    	  if (event.getResults() == null || event.getResults().indexOf("Erro")!=-1){
		    		  Window.alert(new HTML(event.getResults()).getText());
		    		  return;
		    	  }
		    			                          

		        // When the form submission is successfully completed, this event is
		        // fired. Assuming the service returned a response of type text/html,
		        // we can get the result text here (see the FormPanel documentation for
		        // further explanation).
		        //Window.alert(event.getResults());
		        
		        HTML texto = new HTML();
		        texto.setHTML(event.getResults());
		        
		        DadosDaImagem dados = DadosDaImagemClientUtil.fillObject(texto.getText());
		        galeria.adicionaNaGaleria(dados);
		      }
		    });
		    
		    initWidget(form);
		}
	}
	
	public void setEnabled(boolean enabled){
		upload.setEnabled(enabled);
		submit.setEnabled(enabled);
		galeria.setEnabled(enabled);
	}
}
